/* 1652. 拆炸弹 */
/**
 * @param {number[]} code
 * @param {number} k
 * @return {number[]}
 */
var decrypt = function (code, k) {
	const len = code.length
	const result = new Array(len).fill(0)
	const helper = (arr, flag) => {
		for (let i = 0; i < len; i++) {
			let j = flag ? i + 1 : i - 1,
				cur = 0,
				count = 0
			while (count < Math.abs(k)) {
				if (j >= len) {
					j -= len
				}
				if (j < 0) {
					j += len
				}
				cur += code[j]
				j = flag ? j + 1 : j - 1
				count++
			}
			arr[i] = cur
		}
	}
	if (k === 0) return result
	helper(result, k > 0)
	return result
}
/* 滑动窗口 */
var decrypt = function (code, k) {
	const n = code.length
	const res = new Array(n).fill(0)
	if (k === 0) return res
	const newCode = new Array(n * 2).fill(0).map((_, idx) => {
		return code[idx % n]
	})
	code = newCode
	let l = k > 0 ? 1 : n + k
	let r = k > 0 ? k : n - 1
	let w = 0
	for (let i = l; i <= r; i++) {
		w += code[i]
	}
	for (let i = 0; i < n; i++) {
		res[i] = w
		w -= code[l]
		w += code[r + 1]
		l++
		r++
	}

	return res
}

const result = decrypt([2, 4, 9, 3], -2)
